<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <meta name="referrer" content="no-referrer"/>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="./js/vue.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <style>
        .pic{
            width: 50px;
        }
    </style>
</head>
<body>
    <div id="app">
        <h2>豆瓣top250</h2>
        <button @click="handleAdd">添加电影数据</button>
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>编号</th>
                    <th>名字</th>
                    <th>海报</th>
                    <th>评分</th>
                    <th>是否收藏</th>
                    <th>删除</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item of movies" >
                    <td>{{item._id}}</td>
                    <td @click="handleTopages(item._id)">{{item.title}}</td>
                    <td><img class="pic" :src="item.pic" alt=""></td>
                    <td>{{item.rating}}</td>
                    <td>
                        <img @click="handleCollect(item._id)" :src="item.collected?'images/collect.png':'images/cancel.png'">
                    </td>
                    <td>
                        <button type="button" class="btn btn-danger" @click="handleDelete(item._id,item.title)">删除</button>
                    </td>
                </tr>
            </tbody>
        </table>
        <nav aria-label="Page navigation" class="text-center">
            <ul class="pagination pagination-lg">
              <li>
                <a href="javascript:void(0)" aria-label="Previous" @click="handlePrev">
                  <span aria-hidden="true">&laquo;</span>
                </a>
              </li>
                <li v-for="item of pages" @click="handleCound(item)"><a href="javascript:void(0)">{{item}}</a></li>
              <li>
                <a href="javascript:void(0)" aria-label="Next" @click="handleNext">
                  <span aria-hidden="true">&raquo;</span>
                </a>
              </li>
            </ul>
          </nav>
    </div>
    <script>
        new Vue({
            el:"#app",
            data:{
                movies:[],
                total:0,
                item:0
            },
            mounted() {
                $.ajax({
                    url:"http://localhost:8080/top250",
                    success:res=>{
                        this.movies = res.res
                        this.total = res.total
                    }
                })
            },
            methods: {
                async handleCollect(id){
                    var movies = this.movies
                    var item = movies.find(item=>{
                        return item._id == id
                    })
                    if(item.collected){
                        item.collected = false
                        await $.ajax({
                            url:'http://localhost:8080/collect/cancel',
                            data:{id},
                            type:"post"
                        })
                    }else{
                        item.collected = true
                        await $.ajax({url:'http://localhost:8080/collect',data:{id},type:"post"})
                    }
                    this.movies = movies
                },
                async handleDelete(id,title){
                    var movies = this.movies
                    this.movies = movies.filter(item=>{
                        return item._id != id
                    })
                    await $.ajax({url:'http://localhost:8080/Delete',data:{title},type:"post"})
                },
                async handleCound(item){
                    this.item = item
                    var start = (item-1)*5
                    await $.ajax({
                        url:'http://localhost:8080/top250',
                        data:{start},
                        type:"get",
                        success:res=>{
                            this.movies = res.res
                        }
                    })
                    this
                },
                async handleNext(){
                    var item = this.item
                    item += 1
                    if(item>5){
                        item = 5
                    }
                    this.item = item
                    var start = item*5
                    console.log(start);
                    await $.ajax({
                        url:'http://localhost:8080/top250',
                        data:{start},
                        type:"get",
                        success:res=>{
                            this.movies = res.res
                        }
                    })
                },
                async handlePrev(){
                    var item = this.item
                    item -= 1
                    if(item<0){
                        item = 0
                    }
                    this.item = item
                    var start = item*5
                    console.log(start);
                    await $.ajax({
                        url:'http://localhost:8080/top250',
                        data:{start},
                        type:"get",
                        success:res=>{
                            this.movies = res.res
                        }
                    })
                },
                handleTopages(id){
                    location.href=`./html/deteil.html?id=${id}`
                },
                handleAdd(){
                    location.href="./html/add.html"
                }
            },
            computed:{
                pages:function(){
                    var total = this.total
                    var arr = []
                    for(var i=0;i<total;i+=5){
                        arr.push(i)
                    }
                    var res = arr.map(item=>{
                        return (item+5)/5
                    })
                    return res
                }
            }
        })
    </script>
</body>
</html>